package com.leo.consumer.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Map;

@Component
public class SpringRabbitListener {

    /**
     * 利用 @RabbitListener 注解，监听 simple.queue 队列
     */
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMessage(String msg) {
        System.out.println("Spring AMQP 接收到消息: " + msg);
    }

    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1 Spring AMQP 接收到消息: " + msg + " " + LocalTime.now());
        Thread.sleep(20);
    }

    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue2(String msg) throws InterruptedException {
        System.err.println("消费者2 Spring AMQP 接收到消息: " + msg + " " + LocalTime.now());
        Thread.sleep(200);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg) {
        System.out.println("消费者1 Spring AMQP 接收到 fanout 消息: " + msg + " " + LocalTime.now());
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String msg) {
        System.err.println("消费者2 Spring AMQP 接收到 fanout 消息: " + msg + " " + LocalTime.now());
    }

    @RabbitListener(queues = "direct.queue1")
    public void listenDirectQueue1(String msg) {
        System.out.println("消费者1 Spring AMQP 接收到 direct 消息: " + msg + " " + LocalTime.now());
    }

    @RabbitListener(queues = "direct.queue2")
    public void listenDirectQueue2(String msg) {
        System.err.println("消费者2 Spring AMQP 接收到 direct 消息: " + msg + " " + LocalTime.now());
    }

    @RabbitListener(queues = "topic.queue1")
    public void listenTopicQueue1(String msg) {
        System.out.println("消费者1 Spring AMQP 接收到 topic 消息: " + msg + " " + LocalTime.now());
    }

    @RabbitListener(queues = "topic.queue2")
    public void listenTopicQueue2(String msg) {
        System.err.println("消费者2 Spring AMQP 接收到 topic 消息: " + msg + " " + LocalTime.now());
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "listen.direct.queue1", durable = "true"),
            exchange = @Exchange(value = "listen.direct", type = ExchangeTypes.DIRECT),
            key = "listen.routing.key"
    ))
    public void listenDirectQueue3(String msg) {
        System.err.println("消费者 Spring AMQP 接收到 direct 消息: " + msg + " " + LocalTime.now());
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "listen.direct.queue2", durable = "true"),
            exchange = @Exchange(value = "listen.direct", type = ExchangeTypes.DIRECT),
            key = "listen.routing.key"
    ))
    public void listenDirectQueue4(String msg) {
        System.err.println("消费者 Spring AMQP 接收到 direct 消息: " + msg + " " + LocalTime.now());
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "listen.topic.queue1", durable = "true"),
            exchange = @Exchange(value = "listen.topic", type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void listenTopicQueue3(String msg) {
        System.err.println("消费者 Spring AMQP 接收到 topic 消息: " + msg + " " + LocalTime.now());
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "listen.topic.queue2", durable = "true"),
            exchange = @Exchange(value = "listen.topic", type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public void listenTopicQueue4(String msg) {
        System.err.println("消费者 Spring AMQP 接收到 topic 消息: " + msg + " " + LocalTime.now());
    }

    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue(Map<String, Object> msg) {
        System.out.println("消费者 Spring AMQP 接收到object消息: " + msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = "header.direct"),
            value = @Queue(value = "header.direct.queue", durable = "true"),
            key = "header.routing.key"
    ))
    public void listenHeaderQueue(Map<String, Object> msg) {
        System.out.println("消费者 Spring AMQP 接收到 header 消息: " + msg);
    }

    @RabbitListener(queuesToDeclare = @Queue(
            value = "lazy.queue",
            durable = "true",
            arguments = @Argument(name = "x-queue-mode",value = "lazy")
    ))
    public void listenLazyQueue2(String msg) {
        System.out.println("消费者 Spring AMQP 接收到 lazy 消息: " + msg);
    }

    @RabbitListener(queues = "exception.queue")
    public void listenException(String msg) {
        if ("throw".equals(msg)){
            throw new RuntimeException("msg is throw");
        }
        if ("error".equals(msg)){
            throw new MessageConversionException("msg is error");
        }
        System.out.println("Spring AMQP 接收到消息: " + msg);
    }

}
